package commons.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class RiskAnalyser {
	
	
	public static void main(String[] args) {
		if(args.length != 1){
			System.err.println("Usage: <file containing summary per hour>");
			System.exit(1);
		}
		
		try {
			FileWriter writer = new FileWriter(new File("analiseRisco.dat"));

			//File template: 0	Summary	19	19	0	Purch	RES:	ON:	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1
			BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
			
			while(reader.ready()){
				String[] data = reader.readLine().split("\t");
				int amountOfData = data.length;
				
				int instancesNeeded = Integer.valueOf(data[2]);
				int instancesToBuyOrRelease = Integer.valueOf(data[3]);
				int instancesObtained = 0;
				
				if(instancesToBuyOrRelease > 0){//Checking whether instances were successfully obtained
					int index = 7;
					
					while(index < amountOfData && !data[index].startsWith("O")){//Reserved instances
						instancesObtained += Integer.valueOf(data[index]);
						index++;
					}
					
					index++;
					
					//On-demand instances
					for(int i = index; i < amountOfData; i++){
						instancesObtained += Integer.valueOf(data[index]);
					}
					
					writer.write(instancesNeeded+"\t"+instancesToBuyOrRelease+"\t"+instancesObtained+"\t"+(instancesToBuyOrRelease == instancesObtained)+"\n");
//					System.err.println(instancesNeeded+"\t"+instancesObtained+"\t"+(instancesToBuyOrRelease == instancesObtained));
				}
			}
			
			reader.close();
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
